global path "/Users/vedantvohra/Library/CloudStorage/Dropbox/ACR Bounds/Replication Kit" 

clear
cap log close
cd "$path"
local name = "ohie_ed" 

use "$path/Data/data_for_analysis.dta", clear


keep if 1==1  & sample_ed==1

keep ed_visit_30sep2009 ohp_all_ever_30sep2009 ohp_all_mo_30sep2009 nnn* ed_visit_09mar2008 all zero one twoplus twoplus_out fiveplus treatment household_id

*All
ivregress 2sls ed_visit_30sep2009 (ohp_all_mo_30sep2009=treatment) nnn* ed_visit_09mar2008 if all == 1 [pw = 1] , cluster(household_id)

*Non-Zero
ivregress 2sls ed_visit_30sep2009 (ohp_all_mo_30sep2009=treatment) nnn* ed_visit_09mar2008 if zero == 0 [pw = 1] , cluster(household_id)

*Zero
ivregress 2sls ed_visit_30sep2009 (ohp_all_mo_30sep2009=treatment) nnn* ed_visit_09mar2008 if zero == 1 [pw = 1], cluster(household_id)

save "$path/Output/oregon_ed_boot.dta", replace

forval m=1/19 {
	cap drop covered_months_`m'_or_more
	gen covered_months_`m'_or_more = ohp_all_mo_30sep2009 >=`m'
}

*All
preserve

cap mat drop weights_all

forval m=1/19 {
reg covered_months_`m'_or_more treatment nnn* ed_visit_09mar2008 [pw=1], cl(household_id) noomit
mat weights_all = nullmat(weights_all) \ _b[treatment]
}

clear 
set obs 19
gen months = _n
svmat  weights_all
qui sum weights_all1
replace weights_all1 = (weights_all1 / r(sum))

save "$path/Output/`name'_w_all.dta", replace

restore


*Zero
preserve

keep if zero == 1 

cap mat drop weights_zero

forval m=1/19 {
reg covered_months_`m'_or_more treatment nnn* ed_visit_09mar2008 [pw=1], cl(household_id) noomit
mat weights_zero = nullmat(weights_zero) \ _b[treatment]
}

clear 
set obs 19
gen months = _n
svmat  weights_zero
qui sum weights_zero1
replace weights_zero1 = (weights_zero1 / r(sum))

save "$path/Output/`name'_w_zero.dta", replace

restore


*Greater than zero
preserve

keep if zero == 0 

cap mat drop weights_non_zero

forval m=1/19 {
reg covered_months_`m'_or_more treatment nnn* ed_visit_09mar2008 [pw=1], cl(household_id) noomit
mat weights_non_zero = nullmat(weights_non_zero) \ _b[treatment]
}

clear 
set obs 19
gen months = _n
svmat  weights_non_zero
qui sum weights_non_zero1
replace weights_non_zero1 = (weights_non_zero1 / r(sum))

save "$path/Output/`name'_w_nonzero.dta", replace

restore
